package org.example.mrrope.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.mrrope.consts.DingConst;
import org.example.mrrope.dto.ResultPair;
import org.example.mrrope.service.ProductionScheduleService;
import org.example.mrrope.utils.DingUtil;
import org.example.mrrope.utils.FormUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.security.Key;
import java.util.*;


/**
 * @author Lizzy
 * @date 2024/08/30
 * @desc 计划单方法集
 */

@Service
@Slf4j
public class ProductionScheduleServiceImpl implements ProductionScheduleService {

    @Autowired
    private DingUtil dingUtil;
    @Autowired
    private DingConst dingConst;

    @Async
    @Override
    public Integer upsertProductionSchedule(String soNum, String poNum, String userId) {
        //截取传入的SO号，合同号
        String soArrayString = soNum.substring(1, soNum.length() - 1);
        String[] soArray = soArrayString.split(",");
        soArray = Arrays.stream(soArray).map(s -> s.replace("\"", "")).toArray(String[]::new);
        soArray = Arrays.stream(soArray).map(s -> s.replace(" ", "")).toArray(String[]::new);
        String so = Arrays.toString(soArray);
        so = so.substring(1, so.length() - 1);
        poNum = (poNum == null) ? "[\"/\"]" : poNum;
        String poArrayString = poNum.substring(1, poNum.length() - 1);
        String[] poArray = poArrayString.split(",");
        poArray = Arrays.stream(poArray).map(s -> s.replace("\"", "")).toArray(String[]::new);
        String po = Arrays.toString(poArray);
        JSONArray orderDetails = new JSONArray();
        String khmc = "";
        Object date = null;
        try {
            //循环所有的SO号
            for (int j = 0; j < soArray.length; j++) {
                JSONObject searchFieldJson = new JSONObject();
                //查询销售订单
                searchFieldJson.put("textField_lw1zynt8", soArray[j]);
                String yiDaSearchReturnData_XSDD = dingUtil.yiDaSearchReturnData("FORM-E20DB9E153A04B93A66F534532647E4BOL54", searchFieldJson);//获取销售订单数据
                JSONArray yiDaSearchReturnData_XSDDJsonArray = new JSONArray(yiDaSearchReturnData_XSDD);
                JSONObject xsddJSONObject = yiDaSearchReturnData_XSDDJsonArray.getJSONObject(0).getJSONObject("formData");
                String SOID = yiDaSearchReturnData_XSDDJsonArray.getJSONObject(0).getString("formInstanceId");
                //客户简称
                if (xsddJSONObject.has("textField_m2a2vhex")) {
                    khmc = xsddJSONObject.getString("textField_m2a2vhex");
                }
                //交货日期
                if (xsddJSONObject.has("dateField_lup17akb")) {
                    date = xsddJSONObject.get("dateField_lup17akb");
//                    date = dingUtil.getInnerTables(id, "FORM-A79E0E23CFFD49F5B5A802BEDA6B6AF6CCYW", "tableField_ltzkj7lg");//请购单内子表
                }
                //获取订单明细
                if (xsddJSONObject.has("tableField_luouyb0n")) {
                    dingUtil.getInnerTables(SOID, "FORM-E20DB9E153A04B93A66F534532647E4BOL54", "tableField_luouyb0n").forEach(orderDetails::put);
//                    xsddJSONObject.getJSONArray("tableField_luouyb0n").forEach(orderDetails::put);
                }
            }
            JSONArray bomJsonArray = new JSONArray();
            JSONArray acTable = new JSONArray();
//            String[] typeArr = new String[5];
            String type = "成品";
            Map<String, Double> mapOrderNumber = new HashMap<>();
            Map<String, JSONObject> newMergeJsonMap_cp = new HashMap<>();
            //循环所有SO号下的订单明细并合并
            orderDetails.forEach(item -> {
                //赋值到jsonObject
                JSONObject jsonObject = (JSONObject) item;
                //key为编号-AC号/B号/C号/H号
                String key = jsonObject.getString("textField_lw60pi9n");
                //不为空就合并
                if (!key.isEmpty()) {
                    if (newMergeJsonMap_cp.get(key) != null) {
                        // 获取结果参数
                        JSONObject mergeObj = newMergeJsonMap_cp.get(key);
                        BigDecimal ddsl = mergeObj.has("numberField_luouyb0t") ? mergeObj.getBigDecimal("numberField_luouyb0t") : BigDecimal.valueOf(0.0);
                        BigDecimal xs = mergeObj.has("numberField_luouyb0z") ? mergeObj.getBigDecimal("numberField_luouyb0z") : BigDecimal.valueOf(0.0);
                        // 累加
                        ddsl = ddsl.add(jsonObject.has("numberField_luouyb0t") ? jsonObject.getBigDecimal("numberField_luouyb0t") : BigDecimal.valueOf(0.0));
                        xs = xs.add(jsonObject.has("numberField_luouyb0z") ? jsonObject.getBigDecimal("numberField_luouyb0z") : BigDecimal.valueOf(0.0));
                        // 修改
                        mergeObj.put("numberField_luouyb0t", ddsl);
                        mergeObj.put("numberField_luouyb0z", xs);
                        newMergeJsonMap_cp.put(key, mergeObj);
                    } else {
                        // 不存在 复制
                        newMergeJsonMap_cp.put(key, jsonObject);
                    }
                }
            });
            JSONArray newOrderDetails = mapToJsonArray(newMergeJsonMap_cp).getArray();
            //循环合并后的AC，下单数量合并后的AC
            for (int i = 0; i < newOrderDetails.length(); i++) {
                JSONObject orderJo = newOrderDetails.getJSONObject(i);
                //判断是否有箱数，计算是按照箱数算总用发量还是下单数量
                double orderNumber = 0;//下单数量
                double cp_orderNumber = 0;
                double bcp_orderNumber = orderJo.getDouble("numberField_luouyb0t");//订单数量
                if (orderJo.has("numberField_luouyb0z")) {
                    cp_orderNumber = orderJo.getDouble("numberField_luouyb0z");//订单箱数
                    //有箱数就按照箱数计算
                    orderNumber = cp_orderNumber;
                } else {
                    //没有就按照订单数量去计算
                    orderNumber = bcp_orderNumber;
                }
                //成品BOM的查询参数
                JSONObject searchFieldJsonTow = new JSONObject();
                String specifiedChar = "成品";
                //判断类型
                String bomId = "";
                if (orderJo.has("textField_lw60pi9n")) {
                    bomId = orderJo.getString("textField_lw60pi9n");
                    char c = bomId.charAt(0);
                    if (c == 'C') {
                        //绳子
                        specifiedChar = "绳子";
                    } else if (c == 'B') {
                        //半成品
                        specifiedChar = "半成品";
                    } else if (c == 'H') {
                        //绳段
                        specifiedChar = "绳段";
                    }
                    searchFieldJsonTow.put("radioField_lz6dkx3n", specifiedChar);
                    searchFieldJsonTow.put("textField_lujgt576", bomId);
                }
                //调用接口查询成品BOM
                String bomItem = dingUtil.yiDaSearchReturnData("FORM-1302BBEC3D914EBAAF95451A6DCE345ARF8K", searchFieldJsonTow);
                JSONArray bomOneJson = new JSONArray(bomItem);
                //formData==BOM下的所有表单数据
                JSONObject bomJsonObject = bomOneJson.getJSONObject(0).getJSONObject("formData");
                String formInstanceId = bomOneJson.getJSONObject(0).getString("formInstanceId");
                bomJsonObject.put("instanceId", formInstanceId);
//                Arrays.fill(typeArr, bomJsonObject.getString("radioField_lz6dkx3n"));//订单类型
                type = bomJsonObject.getString("radioField_lz6dkx3n");//订单类型
                String title = bomJsonObject.getString("textField_lujgt576");//编号
                JSONArray associationFormField = FormUtils.getAssociationFormField("FORM-1302BBEC3D914EBAAF95451A6DCE345ARF8K", title, formInstanceId);
                //成品BOM信息赋值
                JSONObject acObj = new JSONObject();
                acObj.put("associationFormField_m0wbjlek", associationFormField);
                acObj.put("textField_m0xlus2q", bomJsonObject.getString("textField_lujgt579"));//内部名称
//                acObj.put("numberField_m1hv1yu8", orderJo.get("numberField_luouyb0t"));//订单数量
                Double ddslTow = 0.0;
                if (orderJo.has("numberField_luouyb0t")) {
                    ddslTow = orderJo.getDouble("numberField_luouyb0t");
                }
                //判断单位换算
                if (orderJo.has("selectField_lw60pi9r")) {
                    String unit = orderJo.getString("selectField_lw60pi9r");
                    //判断是不是码，码进行换算
                    if (unit.equals("码") || unit.equals("米")) {
                        //拿到绳子子表的米重
                        JSONArray tableSZ = bomJsonObject.getJSONArray("tableField_lujgt57k");
                        JSONObject jsonObject = tableSZ.getJSONObject(0);
                        //判断有没有绳子子表
                        if (jsonObject.has("textField_lujgt57l")) {
                            String chao = jsonObject.getString("textField_lujgt57l");
                            //判断和成品BOM是不是一个单号
                            if (chao.equals(title)) {
                                //一样的就换算成米重
                                Double mizhon = jsonObject.has("numberField_lujhx4e1") ? jsonObject.getDouble("numberField_lujhx4e1") : 0.0;
                                if (unit.equals("码")) {
                                    ddslTow = (ddslTow * 0.91 * mizhon) / 1000;
                                }
                                if (unit.equals("米")) {
                                    ddslTow = (ddslTow * mizhon) / 1000;
                                }
                                acObj.put("numberField_m1hv1yu8", ddslTow);//订单数量
                                acObj.put("textField_m27bw7dg", "KG");//单位
                                orderNumber = ddslTow;
                            }
                        }
                    } else {
                        // orderJo.get("numberField_luouyb0t")
                        acObj.put("numberField_m1hv1yu8", ddslTow);//订单数量
                        acObj.put("textField_m27bw7dg", unit);//单位
                    }
                } else {
                    acObj.put("numberField_m1hv1yu8", ddslTow);//订单数量
                    acObj.put("textField_m27bw7dg", "个");//单位
                }
                acObj.put("numberField_m28fb0uy", cp_orderNumber);//箱数
                acObj.put("textField_m1kgb2td", title);//AC号
                acObj.put("numberField_m5qo9bht", orderJo.has("numberField_luouyb0x") ? orderJo.get("numberField_luouyb0x") : 0.0);//AC号 numberField_luouyb0x
                acObj.put("imageField_m28dpcz9", bomJsonObject.has("imageField_lujgt57h") ? bomJsonObject.get("imageField_lujgt57h") : "");
                acObj.put("numberField_m2aidy02", orderJo.has("numberField_luouyb0z") ? orderJo.get("numberField_luouyb0z") : 0.0);//总箱数
                acObj.put("numberField_m3hl2xvq", ddslTow);//总未入库数
                acObj.put("numberField_m3hl2xvp", 0.0);//已入库数
                acTable.put(acObj);
                bomJsonArray.put(bomJsonObject);
                mapOrderNumber.put(title, orderNumber);
            }

            String bomJsonArrayString = bomJsonArray.toString();
            //合并各子表
            Map<String, JSONObject> newMergeJsonMap_bcp = new HashMap<>();

            //半成品信息子表
//            if (typeArr.length > 2) {
//                System.out.println("数组长度大于2");
//            }

            if (type.equals("成品")) {
                newMergeJsonMap_bcp = getMergeMap(bomJsonArrayString, "tableField_lwed3a1x",
                        "textField_lujgt592", null, null, "numberField_lxwv7czo", mapOrderNumber, "textField_m25mixp3", "numberField_lxvgkgg7", "bBom", type, "numberField_m5z1tmyo");
            } else {
                newMergeJsonMap_bcp = getMergeMap(bomJsonArrayString, "tableField_lwed3a1x",
                        "textField_lujgt592", null, null, "numberField_lxwv7czo", mapOrderNumber, "textField_lujgt592", "numberField_lxvgkgg7", "bBom", type, "numberField_m5z1tmyo");
            }
            ResultPair resultPair = mapToJsonArray(newMergeJsonMap_bcp);
            Map<String, Double> bcpMap = resultPair.getBcpMap();
            //制绳
            Map<String, JSONObject> newMergeJsonMap_zs = new HashMap<>();
            Map<String, JSONObject> newMergeJsonMap_ms = new HashMap<>();
            if (type.equals("绳子") ) {
//                mapOrderNumber.put("bcpMap",bcpMap.toString());
                newMergeJsonMap_zs = getMergeMap(bomJsonArrayString, "tableField_lujgt57k",
                        "textField_lx07bljm", "textField_lujgt57l", null, "numberField_lxwv7czn",
                        mapOrderNumber, "textField_lujgt57l", "numberField_lz6dt0zh", "rope", type, "numberField_m5z1tmyq");

                ResultPair resultPair_sz = szmapToJsonArray(newMergeJsonMap_zs);
                Map<String, Double> szMap = resultPair_sz.getBcpMap();
                //棉纱
                newMergeJsonMap_ms = getMergeMap(bomJsonArrayString, "tableField_lwg8vm75",
                        "textField_lx07bljn", "textField_lwg902pg", "textField_lwg902p8",
                        "numberField_lxvivx3m", szMap, "textField_lwg902pg", "numberField_lwg902p7", "cotton", type, "numberField_m4hx46ft");
            } else {
                newMergeJsonMap_zs = getMergeMap(bomJsonArrayString, "tableField_lujgt57k",
                        "textField_lx07bljm", "textField_lujgt57l", null, "numberField_lxwv7czn",
                        bcpMap, "textField_lx07bljm", "numberField_lz6dt0zh", "rope", type, "numberField_m5z1tmyq");
                //棉纱
                newMergeJsonMap_ms = getMergeMap(bomJsonArrayString, "tableField_lwg8vm75",
                        "textField_lx07bljn", "textField_lwg902pg", "textField_lwg902p8",
                        "numberField_lxvivx3m", bcpMap, "textField_lx07bljn", "numberField_lwg902pa", "cotton", type, "numberField_m4hx46ft");
            }
            //切绳
            Map<String, JSONObject> newMergeJsonMap_qs = getMergeMap(bomJsonArrayString, "tableField_m0en6p40",
                    "textField_m0en6p3k", "textField_m0en6p42", "textField_m0en6p3l",
                    "numberField_m0en6p3x", bcpMap, "textField_m0en6p3k", "numberField_m27fgapz", "cutting", type, "numberField_m607dwou");
            //配件
            Map<String, JSONObject> newMergeJsonMap_pj = getMergeMap(bomJsonArrayString, "tableField_lvxe56yi",
                    "textField_lx07bljk", "textField_lujgt59f", null, "numberField_lxwv7czp",
                    bcpMap, "textField_lx07bljk", "numberField_lz6dt0zj", "配件", type, "numberField_m4hx46fs");
            //合并工序
            ArrayList<String> listOneGonXu = getgonxuMap(bomJsonArrayString, "checkboxField_lx2luc3u");
            JSONObject upJo = new JSONObject();
            upJo.put("tableField_lujgt57k", mapToJsonArray(newMergeJsonMap_zs).getArray());//制绳
            upJo.put("tableField_lwg8vm75", mapToJsonArray(newMergeJsonMap_ms).getArray());//棉纱
            upJo.put("tableField_m0en6p40", mapToJsonArray(newMergeJsonMap_qs).getArray());//切绳
            upJo.put("tableField_lwed3a1x", resultPair.getArray());//半成品
            upJo.put("tableField_lvxe56yi", mapToJsonArray(newMergeJsonMap_pj).getArray());//配件
            upJo.put("tableField_m0gbvy0a", acTable);//成品
            upJo.put("textField_lujgt578", khmc);//客户名称
            upJo.put("textField_m2e5ira7", po);//合同号
            upJo.put("dateField_lz9hf20u", date);//预计交货日期
            JSONObject searchScheduleJson = new JSONObject();
            searchScheduleJson.put("textField_lwt4u8ls", so);
            String yiDaSearchReturnData_Schedule = dingUtil.yiDaSearchReturnData("FORM-3A8D681A77094B6DAF95EBB52ABF301DGFLW", searchScheduleJson);//获取生产计划数据
            JSONArray yiDaSearchReturnData_ScheduleJsonArray = new JSONArray(yiDaSearchReturnData_Schedule);
            if (!yiDaSearchReturnData_ScheduleJsonArray.isEmpty()) {
                String oid = yiDaSearchReturnData_ScheduleJsonArray.getJSONObject(0).getString("formInstanceId");
                log.info("更新生产计划数据开始");
                log.info("更新oid:{}", oid);
                log.info("更新数据:{}", upJo);
                dingUtil.UpdateYiDa(oid, upJo);//更新生产计划
                log.info("更新生产计划数据完成");
            } else {
                upJo.put("textField_lwt4u8ls", so);//'SO号
                upJo.put("checkboxField_lx2luc3u", listOneGonXu);
                upJo.put("radioField_m1k25hzi", "未下发");//包装派工
                upJo.put("radioField_m1k25hzn", "待下发");//生产派工
                upJo.put("radioField_m2bc8txg", "待下发");//物料采购
                upJo.put("radioField_m2bc8txh", "未下发");//包材采购
                upJo.put("radioField_m2woaxgr", "未分析");//);//包材分析
                dingUtil.saveYiDa("FORM-3A8D681A77094B6DAF95EBB52ABF301DGFLW", upJo, userId);//新增生产计划
                log.info("新增生产计划数据完成");
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        return 0;
    }

    @Async
    @Override
    public Integer upProductionSchedule(String soNum, String poNum, String userId, String JHoid) {
        //截取传入的SO号，合同号
        String soArrayString = soNum.substring(1, soNum.length() - 1);
        String[] soArray = soArrayString.split(",");
        soArray = Arrays.stream(soArray).map(s -> s.replace("\"", "")).toArray(String[]::new);
        soArray = Arrays.stream(soArray).map(s -> s.replace(" ", "")).toArray(String[]::new);
        String so = Arrays.toString(soArray);
        so = so.substring(1, so.length() - 1);
        poNum = (poNum == null) ? "[\"/\"]" : poNum;
        String poArrayString = poNum.substring(1, poNum.length() - 1);
        String[] poArray = poArrayString.split(",");
        poArray = Arrays.stream(poArray).map(s -> s.replace("\"", "")).toArray(String[]::new);
        String po = Arrays.toString(poArray);
        JSONArray orderDetails = new JSONArray();
        String khmc = "";
        Object date = null;
        try {
            //循环所有的SO号
            for (int j = 0; j < soArray.length; j++) {
                JSONObject searchFieldJson = new JSONObject();
                //查询销售订单
                searchFieldJson.put("textField_lw1zynt8", soArray[j]);
                String yiDaSearchReturnData_XSDD = dingUtil.yiDaSearchReturnData("FORM-E20DB9E153A04B93A66F534532647E4BOL54", searchFieldJson);//获取销售订单数据
                JSONArray yiDaSearchReturnData_XSDDJsonArray = new JSONArray(yiDaSearchReturnData_XSDD);
                JSONObject xsddJSONObject = yiDaSearchReturnData_XSDDJsonArray.getJSONObject(0).getJSONObject("formData");
                String SOID = yiDaSearchReturnData_XSDDJsonArray.getJSONObject(0).getString("formInstanceId");
                //客户简称
                if (xsddJSONObject.has("textField_m2a2vhex")) {
                    khmc = xsddJSONObject.getString("textField_m2a2vhex");
                }
                //交货日期
                if (xsddJSONObject.has("dateField_lup17akb")) {
                    date = xsddJSONObject.get("dateField_lup17akb");
//                    date = dingUtil.getInnerTables(id, "FORM-A79E0E23CFFD49F5B5A802BEDA6B6AF6CCYW", "tableField_ltzkj7lg");//请购单内子表
                }
                //获取订单明细
                if (xsddJSONObject.has("tableField_luouyb0n")) {
                    dingUtil.getInnerTables(SOID, "FORM-E20DB9E153A04B93A66F534532647E4BOL54", "tableField_luouyb0n").forEach(orderDetails::put);
//                    xsddJSONObject.getJSONArray("tableField_luouyb0n").forEach(orderDetails::put);
                }
            }
            JSONArray bomJsonArray = new JSONArray();
            JSONArray acTable = new JSONArray();
            String type = "成品";
            Map<String, Double> mapOrderNumber = new HashMap<>();
            Map<String, JSONObject> newMergeJsonMap_cp = new HashMap<>();
            //循环所有SO号下的订单明细并合并
            orderDetails.forEach(item -> {
                //赋值到jsonObject
                JSONObject jsonObject = (JSONObject) item;
                //key为编号-AC号/B号/C号/H号
                String key = jsonObject.getString("textField_lw60pi9n");
                //不为空就合并
                if (!key.isEmpty()) {
                    if (newMergeJsonMap_cp.get(key) != null) {
                        // 获取结果参数
                        JSONObject mergeObj = newMergeJsonMap_cp.get(key);
                        BigDecimal ddsl = mergeObj.has("numberField_luouyb0t") ? mergeObj.getBigDecimal("numberField_luouyb0t") : BigDecimal.valueOf(0.0);
                        BigDecimal xs = mergeObj.has("numberField_luouyb0z") ? mergeObj.getBigDecimal("numberField_luouyb0z") : BigDecimal.valueOf(0.0);
                        // 累加
                        ddsl = ddsl.add(jsonObject.has("numberField_luouyb0t") ? jsonObject.getBigDecimal("numberField_luouyb0t") : BigDecimal.valueOf(0.0));
                        xs = xs.add(jsonObject.has("numberField_luouyb0z") ? jsonObject.getBigDecimal("numberField_luouyb0z") : BigDecimal.valueOf(0.0));
                        // 修改
                        mergeObj.put("numberField_luouyb0t", ddsl);
                        mergeObj.put("numberField_luouyb0z", xs);
                        newMergeJsonMap_cp.put(key, mergeObj);
                    } else {
                        // 不存在 复制
                        newMergeJsonMap_cp.put(key, jsonObject);
                    }
                }
            });
            JSONArray newOrderDetails = mapToJsonArray(newMergeJsonMap_cp).getArray();
            //循环合并后的AC，下单数量合并后的AC
            for (int i = 0; i < newOrderDetails.length(); i++) {
                JSONObject orderJo = newOrderDetails.getJSONObject(i);
                //判断是否有箱数，计算是按照箱数算总用发量还是下单数量
                double orderNumber = 0;//下单数量
                double cp_orderNumber = 0;
                double bcp_orderNumber = orderJo.getDouble("numberField_luouyb0t");//订单数量
                if (orderJo.has("numberField_luouyb0z")) {
                    cp_orderNumber = orderJo.getDouble("numberField_luouyb0z");//订单箱数
                    //有箱数就按照箱数计算
                    orderNumber = cp_orderNumber;
                } else {
                    //没有就按照订单数量去计算
                    orderNumber = bcp_orderNumber;
                }
                //成品BOM的查询参数
                JSONObject searchFieldJsonTow = new JSONObject();
                String specifiedChar = "成品";
                //判断类型
                String bomId = "";
                if (orderJo.has("textField_lw60pi9n")) {
                    bomId = orderJo.getString("textField_lw60pi9n");
                    char c = bomId.charAt(0);
                    if (c == 'C') {
                        //绳子
                        specifiedChar = "绳子";
                    } else if (c == 'B') {
                        //半成品
                        specifiedChar = "半成品";
                    } else if (c == 'H') {
                        //绳段
                        specifiedChar = "绳段";
                    }
                    searchFieldJsonTow.put("radioField_lz6dkx3n", specifiedChar);
                    searchFieldJsonTow.put("textField_lujgt576", bomId);
                }
                //调用接口查询成品BOM
                String bomItem = dingUtil.yiDaSearchReturnData("FORM-1302BBEC3D914EBAAF95451A6DCE345ARF8K", searchFieldJsonTow);
                JSONArray bomOneJson = new JSONArray(bomItem);
                //formData==BOM下的所有表单数据
                JSONObject bomJsonObject = bomOneJson.getJSONObject(0).getJSONObject("formData");
                String formInstanceId = bomOneJson.getJSONObject(0).getString("formInstanceId");
                bomJsonObject.put("instanceId", formInstanceId);//formInstanceId
                type = bomJsonObject.getString("radioField_lz6dkx3n");//订单类型
                String title = bomJsonObject.getString("textField_lujgt576");//编号
                JSONArray associationFormField = FormUtils.getAssociationFormField("FORM-1302BBEC3D914EBAAF95451A6DCE345ARF8K", title, formInstanceId);
                //成品BOM信息赋值
                JSONObject acObj = new JSONObject();
                acObj.put("associationFormField_m0wbjlek", associationFormField);
                acObj.put("textField_m0xlus2q", bomJsonObject.getString("textField_lujgt579"));//内部名称
//                acObj.put("numberField_m1hv1yu8", orderJo.get("numberField_luouyb0t"));//订单数量
                Double ddslTow = 0.0;
                if (orderJo.has("numberField_luouyb0t")) {
                    ddslTow = orderJo.getDouble("numberField_luouyb0t");
                }
                //判断单位换算
                if (orderJo.has("selectField_lw60pi9r")) {
                    String unit = orderJo.getString("selectField_lw60pi9r");
                    //判断是不是码，码进行换算
                    if (unit.equals("码") || unit.equals("米")) {
                        //拿到绳子子表的米重
                        JSONArray tableSZ = bomJsonObject.getJSONArray("tableField_lujgt57k");
                        JSONObject jsonObject = tableSZ.getJSONObject(0);
                        //判断有没有绳子子表
                        if (jsonObject.has("textField_lujgt57l")) {
                            String chao = jsonObject.getString("textField_lujgt57l");
                            //判断和成品BOM是不是一个单号
                            if (chao.equals(title)) {
                                //一样的就换算成米重
                                Double mizhon = jsonObject.has("numberField_lujhx4e1") ? jsonObject.getDouble("numberField_lujhx4e1") : 0.0;
                                if (unit.equals("码")) {
                                    ddslTow = (ddslTow * 0.91 * mizhon) / 1000;
                                }
                                if (unit.equals("米")) {
                                    ddslTow = (ddslTow * mizhon) / 1000;
                                }
                                acObj.put("numberField_m1hv1yu8", ddslTow);//订单数量
                                acObj.put("textField_m27bw7dg", "KG");//单位
                                orderNumber = ddslTow;
                            }
                        }
                    } else {
                        acObj.put("numberField_m1hv1yu8", ddslTow);//订单数量
                        acObj.put("textField_m27bw7dg", unit);//单位
                    }
                } else {
                    acObj.put("numberField_m1hv1yu8", ddslTow);//订单数量
                    acObj.put("textField_m27bw7dg", "个");//单位
                }
                acObj.put("numberField_m28fb0uy", cp_orderNumber);//箱数
                acObj.put("textField_m1kgb2td", title);//AC号
                acObj.put("imageField_m28dpcz9", bomJsonObject.has("imageField_lujgt57h") ? bomJsonObject.get("imageField_lujgt57h") : "");
                acObj.put("numberField_m2aidy02", orderJo.has("numberField_luouyb0z") ? orderJo.get("numberField_luouyb0z") : 0.0);//总箱数
                acObj.put("numberField_m3hl2xvq", ddslTow);//总未入库数
                acObj.put("numberField_m3hl2xvp", 0.0);//已入库数
                acTable.put(acObj);
                bomJsonArray.put(bomJsonObject);
                mapOrderNumber.put(title, orderNumber);
            }

            String bomJsonArrayString = bomJsonArray.toString();
            //合并各子表
            Map<String, JSONObject> newMergeJsonMap_bcp = new HashMap<>();

            //半成品信息子表
            if (type.equals("成品")) {
                newMergeJsonMap_bcp = getMergeMap(bomJsonArrayString, "tableField_lwed3a1x",
                        "textField_lujgt592", null, null, "numberField_lxwv7czo", mapOrderNumber, "textField_m25mixp3", "numberField_lxvgkgg7", "bBom", type, "numberField_m5z1tmyo");
            } else {
                newMergeJsonMap_bcp = getMergeMap(bomJsonArrayString, "tableField_lwed3a1x",
                        "textField_lujgt592", null, null, "numberField_lxwv7czo", mapOrderNumber, "textField_lujgt592", "numberField_lxvgkgg7", "bBom", type, "numberField_m5z1tmyo");
            }
            ResultPair resultPair = mapToJsonArray(newMergeJsonMap_bcp);
            Map<String, Double> bcpMap = resultPair.getBcpMap();
            //制绳
            Map<String, JSONObject> newMergeJsonMap_zs = new HashMap<>();
            Map<String, JSONObject> newMergeJsonMap_ms = new HashMap<>();
            if (type.equals("绳子")) {
                mapOrderNumber.put("bL",bcpMap.get("bL"));
                newMergeJsonMap_zs = getMergeMap(bomJsonArrayString, "tableField_lujgt57k",
                        "textField_lx07bljm", "textField_lujgt57l", null, "numberField_lxwv7czn",
                        mapOrderNumber, "textField_lujgt57l", "numberField_lz6dt0zh", "rope", type, "numberField_m5z1tmyq");

                ResultPair resultPair_sz = szmapToJsonArray(newMergeJsonMap_zs);
                Map<String, Double> szMap = resultPair_sz.getBcpMap();
                //棉纱
                newMergeJsonMap_ms = getMergeMap(bomJsonArrayString, "tableField_lwg8vm75",
                        "textField_lx07bljn", "textField_lwg902pg", "textField_lwg902p8",
                        "numberField_lxvivx3m", szMap, "textField_lwg902pg", "numberField_lwg902p7", "cotton", type, "numberField_m4hx46ft");
            } else {
                newMergeJsonMap_zs = getMergeMap(bomJsonArrayString, "tableField_lujgt57k",
                        "textField_lx07bljm", "textField_lujgt57l", null, "numberField_lxwv7czn",
                        bcpMap, "textField_lx07bljm", "numberField_lz6dt0zh", "rope", type, "numberField_m5z1tmyq");
                //棉纱
                newMergeJsonMap_ms = getMergeMap(bomJsonArrayString, "tableField_lwg8vm75",
                        "textField_lx07bljn", "textField_lwg902pg", "textField_lwg902p8",
                        "numberField_lxvivx3m", bcpMap, "textField_lx07bljn", "numberField_lwg902pa", "cotton", type, "numberField_m4hx46ft");
            }
            //切绳
            Map<String, JSONObject> newMergeJsonMap_qs = getMergeMap(bomJsonArrayString, "tableField_m0en6p40",
                    "textField_m0en6p3k", "textField_m0en6p42", "textField_m0en6p3l",
                    "numberField_m0en6p3x", bcpMap, "textField_m0en6p3k", "numberField_m27fgapz", "cutting", type, "numberField_m607dwou");
            //配件
            Map<String, JSONObject> newMergeJsonMap_pj = getMergeMap(bomJsonArrayString, "tableField_lvxe56yi",
                    "textField_lx07bljk", "textField_lujgt59f", null, "numberField_lxwv7czp",
                    bcpMap, "textField_lx07bljk", "numberField_lz6dt0zj", "配件", type, "numberField_m4hx46fs");
            //合并工序
            ArrayList<String> listOneGonXu = getgonxuMap(bomJsonArrayString, "checkboxField_lx2luc3u");
            JSONObject upJo = new JSONObject();
            upJo.put("tableField_lujgt57k", mapToJsonArray(newMergeJsonMap_zs).getArray());//制绳
            upJo.put("tableField_lwg8vm75", mapToJsonArray(newMergeJsonMap_ms).getArray());//棉纱
            upJo.put("tableField_m0en6p40", mapToJsonArray(newMergeJsonMap_qs).getArray());//切绳
            upJo.put("tableField_lwed3a1x", resultPair.getArray());//半成品
            upJo.put("tableField_lvxe56yi", mapToJsonArray(newMergeJsonMap_pj).getArray());//配件
            upJo.put("tableField_m0gbvy0a", acTable);//成品
            upJo.put("textField_lujgt578", khmc);//客户名称
            upJo.put("textField_m2e5ira7", po);//合同号
            upJo.put("dateField_lz9hf20u", date);//预计交货日期
            dingUtil.UpdateYiDa(JHoid, upJo);//更新生产计划JHoid
            log.info("更新生产计划数据完成");
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        return 0;
    }

    /**
     * 更新包材子表
     *
     * @param soNum
     * @return
     */
    @Override
    @Async
    public Integer supplementPackingProductionSchedule(String soNum) {
//        String soArrayString = soNum.substring(1, soNum.length() - 1);
        String[] soArray = soNum.split(",");
        soArray = Arrays.stream(soArray).map(s -> s.replace("\"", "")).toArray(String[]::new);
        soArray = Arrays.stream(soArray).map(s -> s.replace(" ", "")).toArray(String[]::new);
        JSONArray orderDetails = new JSONArray();
        try {
            for (int j = 0; j < soArray.length; j++) {
                JSONObject searchFieldJson = new JSONObject();
                searchFieldJson.put("textField_lw1zynt8", soArray[j]);
                log.info("查询销售订单数据开始:{}", soArray[j]);
                String yiDaSearchReturnData_XSDD = dingUtil.yiDaSearchReturnData("FORM-E20DB9E153A04B93A66F534532647E4BOL54", searchFieldJson);//获取销售订单数据
                JSONArray yiDaSearchReturnData_XSDDJsonArray = new JSONArray(yiDaSearchReturnData_XSDD);
                JSONObject xsddJSONObject = yiDaSearchReturnData_XSDDJsonArray.getJSONObject(0).getJSONObject("formData");
                String SOID = yiDaSearchReturnData_XSDDJsonArray.getJSONObject(0).getString("formInstanceId");
//                if (xsddJSONObject.has("tableField_luouyb0n")) {
//                    xsddJSONObject.getJSONArray("tableField_luouyb0n").forEach(orderDetails::put);
//                }
                if (xsddJSONObject.has("tableField_luouyb0n")) {
                    dingUtil.getInnerTables(SOID, "FORM-E20DB9E153A04B93A66F534532647E4BOL54", "tableField_luouyb0n").forEach(orderDetails::put);
                }
            }
            Map<String, JSONObject> bomJsonMap = new HashMap<>();
            Map<String, Double> mapOrderNumber = new HashMap<>();
            double orderNumber = 0;
            for (int i = 0; i < orderDetails.length(); i++) {
                JSONObject orderJo = orderDetails.getJSONObject(i);
                double cp_orderNumber = orderJo.has("numberField_luouyb0z") ? orderJo.getDouble("numberField_luouyb0z") : 0;//订单箱数
                double bcp_orderNumber = orderJo.getDouble("numberField_luouyb0t");//订单数量
                String substring = orderJo.getString("associationFormField_lwiybpe6_id").replaceAll("\\\\", "").substring(1);
                String instanceId = new JSONArray(substring.substring(0, substring.length() - 1)).getJSONObject(0).getString("instanceId");
                if (instanceId != null) {
                    //bom表
                    String bomItem = dingUtil.getYidaById(instanceId);
                    JSONObject bomJsonObject = new JSONObject(bomItem);
                    bomJsonObject.put("instanceId", instanceId);
                    String type = bomJsonObject.getString("radioField_lz6dkx3n");//订单类型
                    if (type.equals("成品")) {
                        orderNumber = cp_orderNumber;
                    } else {
                        orderNumber = bcp_orderNumber;
                    }
                    bomJsonMap.put(instanceId, bomJsonObject);
                    if (mapOrderNumber.containsKey(orderJo.getString("textField_lw60pi9n"))) {
                        mapOrderNumber.put(orderJo.getString("textField_lw60pi9n"), mapOrderNumber.get(orderJo.getString("textField_lw60pi9n")) + orderNumber);
                    } else {
                        mapOrderNumber.put(orderJo.getString("textField_lw60pi9n"), orderNumber);
                    }
                }
            }

            String bomJsonArrayString = mapToJsonArray(bomJsonMap).getArray().toString();
            //合并包装子表
            //包装
            Map<String, JSONObject> newMergeJsonMap1 = getMergeMap(bomJsonArrayString, "tableField_lxvknj7i",
                    "textField_m0gcswvr", "textField_lxvknj79", null, "numberField_lxwv7czm", mapOrderNumber, "textField_m0gcswvr", "numberField_lz6f59g5", "包材", "", "numberField_m4hx46fr");
            JSONObject upJo = new JSONObject();
            JSONArray array = mapToJsonArray(newMergeJsonMap1).getArray();
            upJo.put("tableField_lxvknj7i", array);//包装
            JSONObject searchScheduleJson = new JSONObject();
            searchScheduleJson.put("textField_lwt4u8ls", soNum);
            log.info("搜索待更新计划单，搜索条件:{}", soNum);
            String yiDaSearchReturnData_Schedule = dingUtil.yiDaSearchReturnData("FORM-3A8D681A77094B6DAF95EBB52ABF301DGFLW", searchScheduleJson);//获取生产计划数据
            JSONArray yiDaSearchReturnData_ScheduleJsonArray = new JSONArray(yiDaSearchReturnData_Schedule);
            String oid = yiDaSearchReturnData_ScheduleJsonArray.getJSONObject(0).getString("formInstanceId");
            log.info("更新生产计划包装子表数据开始");
            log.info("更新oid:{}", oid);
            log.info("更新数据:{}", upJo);
            dingUtil.UpdateYiDa(oid, upJo);//新增生产计划
            log.info("更新生产计划包装子表数据完成");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return 0;
    }


    private static ResultPair mapToJsonArray(Map<String, JSONObject> newMergeJsonMap1) {
        HashMap<String, Double> map = new HashMap<>();
        JSONArray jsonArray = new JSONArray();
        for (Map.Entry<String, JSONObject> entry : newMergeJsonMap1.entrySet()) {
            JSONObject value = entry.getValue();
            if (value.has("textField_lujgt592")) {
                map.put(value.getString("textField_lujgt592"), value.has("numberField_lxwv7czo") ? value.getDouble("numberField_lxwv7czo") : 0.0);
            }
            jsonArray.put(value);
        }
        return new ResultPair(map, jsonArray);
    }

    private static ResultPair szmapToJsonArray(Map<String, JSONObject> newMergeJsonMap1) {
        HashMap<String, Double> map = new HashMap<>();
        JSONArray jsonArray = new JSONArray();
        for (Map.Entry<String, JSONObject> entry : newMergeJsonMap1.entrySet()) {
            JSONObject value = entry.getValue();
            if (value.has("textField_lujgt57l")) {
                map.put(value.getString("textField_lujgt57l"), value.has("numberField_lxwv7czn") ? value.getDouble("numberField_lxwv7czn") : 0.0);
            }
            jsonArray.put(value);
        }
        return new ResultPair(map, jsonArray);
    }


    private BigDecimal getOrderNumber(Double orderJo) {
        BigDecimal bd = new BigDecimal(Double.toString(orderJo));
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        return bd;
    }

    /**
     * @return : Map<String,JSONObject>
     * @Description : 根据组件匹配合并相同列的数量
     * @Author : Lizzy
     * @Param : bomJsonArrayString
     * @Date : 2024/9/25 23:48
     */
    private Map<String, JSONObject> getMergeMap(String bomJsonArrayString, String tableFieldStr, String textFieldStr1,
                                                String textFieldStr2, String textFieldStr3, String numberFieldStr, Map<String, Double> mapOrderNumber, String textFieldStr4, String numberFieldStr2, String type, String ddType, String numberFieldStr3) {
        JSONArray bomJsonArray = new JSONArray(bomJsonArrayString);
        JSONArray mergeJsonArray = new JSONArray();
        bomJsonArray.forEach(item -> {
            JSONObject formDataJsonObj = (JSONObject) item;
            JSONArray tableFieldJsonArray = new JSONArray();
            if (formDataJsonObj.has("instanceId")) {
                try {
                    tableFieldJsonArray = dingUtil.getInnerTables(formDataJsonObj.getString("instanceId"), "FORM-1302BBEC3D914EBAAF95451A6DCE345ARF8K", tableFieldStr);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            } else {
                if (formDataJsonObj.has(tableFieldStr)) {
                    tableFieldJsonArray = formDataJsonObj.getJSONArray(tableFieldStr);
                }
            }
            mergeJsonArray.putAll(tableFieldJsonArray);
        });


        JSONArray newMergeJsonArray = new JSONArray();
        Map<String, JSONObject> newBCPMergeJsonMap = new HashMap<>();
        for (int i = 0; i < mergeJsonArray.length(); i++) {
            JSONObject jsonObject = mergeJsonArray.getJSONObject(i);
            String cfNum = jsonObject.getString(textFieldStr4);//拆分的物料num，即ac或者半成品
            Double orderNum = 1.0;
            if (cfNum == null || cfNum.isEmpty() || mapOrderNumber == null) {
                orderNum = 1.0;
            } else {
                if (mapOrderNumber.get(cfNum) == null) {
                    orderNum = 1.0;
                } else {
                    orderNum = mapOrderNumber.get(cfNum);
                }
            }
            String key = "";
            if (textFieldStr2 != null) {
                key = jsonObject.getString(textFieldStr1) + jsonObject.getString(textFieldStr2);
            }
            if (textFieldStr3 != null) {
                key = key + jsonObject.getString(textFieldStr3);
            }
            //判断半成品有没有值

            log.info("成品{},物料:{}", cfNum, key);
            if (jsonObject.has(numberFieldStr2)) {
                double zyfl = 0.0;

                if (type.equals("cutting")) {
                    //标准用量（根数） * 半成品总应发量 * 切绳重量
                    zyfl = jsonObject.getDouble(numberFieldStr2) * orderNum * (jsonObject.has("numberField_m0en6p3s") ? jsonObject.getDouble("numberField_m0en6p3s") : 0.0);
                    zyfl = zyfl / 1000L;
//                    jsonObject.put("numberField_m607dwou",getOrderNumber(zyfl));
                } else {
                    //标准用量 * 总应发量
                    zyfl = jsonObject.getDouble(numberFieldStr2) * orderNum;
                }
                if (ddType.equals("绳子") && !type.equals("bBom")) {
                    //换算单位 * 1000
                    zyfl = zyfl * 1000;
//                    jsonObject.put("numberField_m5z1tmyq", getOrderNumber(zyfl));
                }
                if (type.equals("rope")) {
                    if (jsonObject.has("textField_lx07bljm")) {
                        //半成品有值则拿到半成品的总应发量
                    }
                    zyfl = zyfl / 1000L;
//                    jsonObject.put("numberField_m5z1tmyq", getOrderNumber(zyfl));
                }
                if (type.equals("cotton")) {
                    zyfl = zyfl / 1000L;
                    if (ddType.equals("绳子")) {
                        zyfl = zyfl / 100L;
                    }
//                    jsonObject.put("numberField_m4hx46ft", getOrderNumber(zyfl));
                }
//                if (type.equals("配件")) {
//                    jsonObject.put("numberField_m4hx46fs", getOrderNumber(zyfl));
//                }
//                if (type.equals("包材")) {
//                    jsonObject.put("numberField_m4hx46fr", getOrderNumber(zyfl));
//                }
//                if (type.equals("bBom")) {
//                    jsonObject.put("numberField_m5z1tmyo", getOrderNumber(zyfl));
//                }
                //保留2位小数
//                BigDecimal bd = new BigDecimal(Double.toString(zyfl));
//                bd = bd.setScale(2, RoundingMode.HALF_UP);
//                double roundedNumber = bd.doubleValue();
                jsonObject.put(numberFieldStr, getOrderNumber(zyfl));
                jsonObject.put(numberFieldStr3, getOrderNumber(zyfl));
                jsonObject.put(numberFieldStr + "_value", getOrderNumber(zyfl));
                jsonObject.put(numberFieldStr3 + "_value", getOrderNumber(zyfl));
            }
            if (!key.isEmpty()) {
                if (newBCPMergeJsonMap.get(key) == null) {
                    newMergeJsonArray.put(jsonObject);
                    newBCPMergeJsonMap.put(key, jsonObject);
                }
            } else {
                newMergeJsonArray.put(jsonObject);
            }
        }
        log.info("合并后的数据：{}", newMergeJsonArray);

        Map<String, JSONObject> newMergeJsonMap = new HashMap<>();
        // 去重合并相加
        newMergeJsonArray.forEach(item -> {
            JSONObject jsonObject = (JSONObject) item;
            String key = jsonObject.getString(textFieldStr1);
            if (textFieldStr2 != null) {
                key = key + jsonObject.getString(textFieldStr2);
            }
            if (textFieldStr3 != null) {
                key = key + jsonObject.getString(textFieldStr3);
            }
            if (!key.isEmpty()) {
                if (newMergeJsonMap.get(key) != null) {
                    // 获取结果参数
                    JSONObject mergeObj = newMergeJsonMap.get(key);
                    if (mergeObj.has(numberFieldStr)) {
                        BigDecimal numberField = mergeObj.getBigDecimal(numberFieldStr);
                        // 累加
                        numberField = numberField.add(jsonObject.has(numberFieldStr) ? jsonObject.getBigDecimal(numberFieldStr) : BigDecimal.valueOf(0.0));
//                        numberField = numberField.add(BigDecimal.valueOf(100.0));
                        // 修改
//                        if (type.equals("rope") || type.equals("cotton")){
//                            numberField = numberField/1000;
//                        }
                        mergeObj.put(numberFieldStr, numberField);
                        log.info("key:{},合并后:{}", key, numberField);
                    }
                    if (mergeObj.has(numberFieldStr3)) {
                        BigDecimal numberField = mergeObj.getBigDecimal(numberFieldStr3);
                        // 累加
                        numberField = numberField.add(jsonObject.has(numberFieldStr3) ? jsonObject.getBigDecimal(numberFieldStr3) : BigDecimal.valueOf(0.0));
//                        numberField = numberField.add(BigDecimal.valueOf(100.0));
                        // 修改
//                        if (type.equals("rope") || type.equals("cotton")){
//                            numberField = numberField/1000;
//                        }
                        mergeObj.put(numberFieldStr3, numberField);
                        log.info("key:{},合并后:{}", key, numberField);
                    }
//                    if (mergeObj.has(numberFieldStr2)){
//
//                    }
                    newMergeJsonMap.put(key, mergeObj);
                    log.info("key:{},合并后:{}", key, mergeObj);
                } else {
                    // 不存在 复制
                    newMergeJsonMap.put(key, jsonObject);
//                    log.info("key:{},应发量:{}", key, jsonObject.get(numberFieldStr));
                }
            }
        });
        return newMergeJsonMap;
    }

    private static ArrayList<String> getgonxuMap(String bomJsonArrayString, String gonxu) {
        JSONArray bomJsonArray = new JSONArray(bomJsonArrayString);
        ArrayList<String> arrayList = new ArrayList<>();
        JSONArray mergeJsonArray = new JSONArray();
        bomJsonArray.forEach(item -> {
            JSONObject formDataJsonObj = (JSONObject) item;
            if (formDataJsonObj.has(gonxu)) {
                JSONArray jsonArray = formDataJsonObj.getJSONArray(gonxu);
                mergeJsonArray.putAll(jsonArray);
            }
        });
        mergeJsonArray.forEach(item -> {
            arrayList.add(item.toString());
        });
        // 去重合并相加
        // 创建HashSet并添加ArrayList中的元素
        HashSet<String> set = new HashSet<>(arrayList);
        // 创建新的ArrayList
        return new ArrayList<>(set);
    }


    @Override
    public void updateWLVersion(String soNum) throws Exception {
//        循环so子表
//        String soBiao= dingUtil.getYidaById(soNum);
//        JSONObject jsonObjectSo= new JSONObject(soBiao);
        JSONObject upJo = new JSONObject();
        //获取子表的数据
        JSONArray jsonArrTow = dingUtil.getInnerTables(soNum, "FORM-E20DB9E153A04B93A66F534532647E4BOL54", "tableField_luouyb0n");
        ArrayList<JSONObject> lisTow = new ArrayList<>();
        for (int l = 0; l < jsonArrTow.length(); l++) {
            JSONObject jsonObjectTow = jsonArrTow.getJSONObject(l);
            //调用接口查询成品BOM
            JSONObject searchFieldJsonTow = new JSONObject();
            String specifiedChar = "成品";
            searchFieldJsonTow.put("radioField_lz6dkx3n", specifiedChar);
            searchFieldJsonTow.put("textField_lujgt576", jsonObjectTow.get("textField_lw60pi9n"));
            String bomItem = dingUtil.yiDaSearchReturnData("FORM-1302BBEC3D914EBAAF95451A6DCE345ARF8K", searchFieldJsonTow);
            JSONArray bomOneJson = new JSONArray(bomItem);
            JSONObject acObj = new JSONObject();
            for (int i = 0; i < bomOneJson.length(); i++) {
                JSONObject bomJsonObject = bomOneJson.getJSONObject(i).getJSONObject("formData");
                String instanceId = bomOneJson.getJSONObject(0).getString("formInstanceId");
                //查询半成品下的规格描述
                if (bomJsonObject.has("textareaField_m1dtl54p")) {
                    if (!bomJsonObject.has("textField_m5516yqt")) {
                        acObj.put("textField_m5516yqt", bomJsonObject.getString("textareaField_m1dtl54p"));
                    }
                }
                if (bomJsonObject.has("associationFormField_lwbmqipe_id")) {
                    String substring = bomJsonObject.getString("associationFormField_lwbmqipe_id").replaceAll("\\\\", "").substring(1);
                    substring.substring(0, substring.length() - 1);
                    JSONArray jsonArray = new JSONArray(substring);
                    if (!jsonArray.isEmpty()) {
                        String ggms = "";
                        for (int k = 0; k < jsonArray.length(); k++) {
                            //查询半成品B号
                            JSONObject jsonObject = jsonArray.getJSONObject(k);
                            String instanceId1 = jsonObject.getString("instanceId");
                            String bomBHaoItem = dingUtil.getYidaById(instanceId1);
                            JSONObject bomJsonObjectTow = new JSONObject(bomBHaoItem);
                            ggms = "【" + ggms + bomJsonObjectTow.get("textareaField_m2ohowzw") + "】" + "; ";

                        }
                        acObj.put("textareaField_m1dtl54p", ggms);
                        jsonObjectTow.put("textareaField_m2okbarf", ggms);
                    }
                }
                dingUtil.UpdateYiDa(instanceId, acObj);
            }
            lisTow.add(jsonObjectTow);
        }
        upJo.put("tableField_luouyb0n", lisTow);
        dingUtil.UpdateYiDa(soNum, upJo);//修改
    }

    @Override
    public void updateWLVersionTOw(String so, String soTOw) throws Exception {
        //查询采购入库状态
        String soBiao = dingUtil.getYidaById(so);
        JSONArray jsonArr = dingUtil.getInnerTables(soTOw, "FORM-A79E0E23CFFD49F5B5A802BEDA6B6AF6CCYW", "tableField_ltzkj7lg");
        ArrayList<JSONObject> lisTow = new ArrayList<>();
        JSONObject jsonObjectSo = new JSONObject(soBiao);
        JSONObject upJo = new JSONObject();
        for (int l = 0; l < jsonArr.length(); l++) {
            JSONObject jsonObject = jsonArr.getJSONObject(l);
            if (jsonObjectSo.has("tableField_lup0qelv")) {
                JSONArray jsonArrTow = jsonObjectSo.getJSONArray("tableField_m24mjsio");
                jsonArrTow.forEach(item -> {
                    JSONObject jsonObjectTow = (JSONObject) item;
                    if (jsonObjectTow.get("textField_lup0qelz").equals(jsonObject.get("textField_ltzkj7li"))) {
                        jsonObject.put("textField_lye3u67v", "已采购");
                    }
                });
            } else if (jsonObjectSo.has("tableField_m24mjsio")) {
                JSONArray jsonArrTow = jsonObjectSo.getJSONArray("tableField_m24mjsio");
                jsonArrTow.forEach(item -> {
                    JSONObject jsonObjectTow = (JSONObject) item;
                    if (jsonObjectTow.get("textField_m24mjsip").equals(jsonObject.get("textField_ltzkj7li"))) {
                        jsonObject.put("textField_lye3u67v", "已采购");
                    }
                });
            }
            lisTow.add(jsonObject);
        }
        upJo.put("tableField_ltzkj7lg", lisTow);
        dingUtil.UpdateYiDa(so, upJo);
    }

    @Override
    public void updateWLVersionBaoZhuang(String instanceId) throws Exception {
        JSONObject upJo = new JSONObject();
        //查询销售订单的子表
        JSONArray jsonArrTow = dingUtil.getInnerTables(instanceId, "FORM-E20DB9E153A04B93A66F534532647E4BOL54", "tableField_luouyb0n");
        ArrayList<JSONObject> lisTow = new ArrayList<>();
        for (int l = 0; l < jsonArrTow.length(); l++) {
            JSONObject jsonObjectTow = jsonArrTow.getJSONObject(l);
            //调用接口查询成品BOM
            JSONObject searchFieldJsonTow = new JSONObject();
            String specifiedChar = "成品";
            searchFieldJsonTow.put("radioField_lz6dkx3n", specifiedChar);
            searchFieldJsonTow.put("textField_lujgt576", jsonObjectTow.get("textField_lw60pi9n"));//ac号
            String bomItem = dingUtil.yiDaSearchReturnData("FORM-1302BBEC3D914EBAAF95451A6DCE345ARF8K", searchFieldJsonTow);
            JSONArray bomOneJson = new JSONArray(bomItem);
            //循环插入到子表
            JSONObject bomJsonObject = bomOneJson.getJSONObject(0).getJSONObject("formData");
            //查询半成品下的包装方式
            if (bomJsonObject.has("textareaField_lvxftd6b")) {
                jsonObjectTow.put("textareaField_lwiybpe8", bomJsonObject.getString("textareaField_lvxftd6b"));
            }
            lisTow.add(jsonObjectTow);

        }
        upJo.put("tableField_luouyb0n", lisTow);
        dingUtil.UpdateYiDa(instanceId, upJo);//更新销售订单
    }
}
